var mysql = require('mysql');

var DBHelper = (function () {

    function DBHelper() {

    }

    DBHelper.prototype.init = function () {
        var connection = null;
        if (!this.connection) {
            connection = mysql.createConnection({
                host: 'localhost',
                user: 'root',
                password: 'root',
                database: 'lu'
            });
            this.connection = connection;
        }
        return connection;
    }

    DBHelper.prototype.createNewTable = function () {
        this.connection.query("CREATE TABLE newtable (id  int NOT NULL AUTO_INCREMENT, name  varchar(255) NOT NULL , des  varchar(255) NULL ,createTime  datetime NULL , updateTime  datetime NULL ,PRIMARY KEY (id));", function (error, results, fields) {
            if (error) throw error;
            console.log(results);
        });
    }
    DBHelper.prototype.renameTable = function () {
        this.connection.query("rename table NewTable to test;", function (error, results, fields) {
            if (error) throw error;
            // console.log(results);
        });
    }
    DBHelper.prototype.insertInfo = function () {
        for (var i = 0; i < 10; i++) {
            this.connection.query("insert into test (name) values ('呵呵哒');", function (error, results, fields) {
                if (error) throw error
            });
        }
    }

    DBHelper.prototype.updateInfo = function () {
        this.connection.query("UPDATE test SET name = ?,des = ? WHERE id = ?", ["hehe", "尼玛兽",5], function (error, results, fields) {
            if (error) throw error
        });
    }

    DBHelper.prototype.deleteInfo = function () {
        this.connection.query("delete from test WHERE id = ?", [1], function (error, results, fields) {
            if (error) throw error
        });
    }
    DBHelper.prototype.dropTable = function () {
        this.connection.query("drop table test;", function (error, results, fields) {
            if (error) throw error
        });
    }

    return DBHelper;
}());

var dbHelper = new DBHelper();
var con = dbHelper.init();
dbHelper.createNewTable();
dbHelper.renameTable();
dbHelper.insertInfo();
dbHelper.updateInfo();
dbHelper.deleteInfo();

// dbHelper.dropTable();
con .end();